package com.xx.pmis.web.util;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;

import com.xx.pmis.model.Present;
import com.xx.pmis.model.util.PresentMonth;
import com.xx.pmis.services.PresentService;

public class PresentUtil extends PMISBaseUtil {
	private static final Log log = LogFactory.getLog(PresentUtil.class);

	PresentService presentService = getPMISServices().getPresentService();

	// 最近十二个月的购买商品支出汇总
	public List<PresentMonth> getPresentOfTwelveMonths() {
		List<PresentMonth> pmList = new ArrayList<PresentMonth>();
		// 服饰鞋帽
		pmList.add(getPresentMonth());
		// 服饰鞋帽
		pmList.add(getPresentMonthByType("0"));
		// 食品茶饮
		pmList.add(getPresentMonthByType("1"));
		// 日常用品
		pmList.add(getPresentMonthByType("2"));
		// 数码家电
		pmList.add(getPresentMonthByType("3"));
		// 其它
		pmList.add(getPresentMonthByType("5"));
		// 合计
		pmList.add(getPresentMonthByYearAndMonth());
		return pmList;
	}

	// 最近5年商品/网购支出情况汇总
	public List<PresentMonth> getPresentOfLastFiveYear() {
		List<PresentMonth> pmList = new ArrayList<PresentMonth>();
		pmList.add(getMonth());

		String currentDate = CurrentDate.currentDate();
		String currentYear = currentDate.substring(0, 4);

		int year = Integer.parseInt(currentYear);
		for (int i = 0; i < 5; i++) {
			pmList.add(getPresentMonthByYear(year - i));
		}
		return pmList;
	}

	// 根据商品类型查询
	public PresentMonth getPresentMonthByType(String type) {
		String currentDate = CurrentDate.currentDate();
		String currentMonth = currentDate.substring(5, 7);
		String currentYear = currentDate.substring(0, 4);

		int year = Integer.parseInt(currentYear);
		int month = Integer.parseInt(currentMonth);
		String y, m;

		PresentMonth pm = new PresentMonth();
		String[] param = new String[12];

		for (int i = 0; i < 12; i++) {
			double typeTotal = 0.00;
			Present present = new Present();
			if (i != 0) {
				month = month - 1;
			}
			if (month == 0) {
				month = 12;
				year = year - 1;
			}

			y = String.valueOf(year);
			m = String.valueOf(month);
			if (m.length() == 1) {
				m = 0 + m;
			}
			// 当前月
			List<Present> presentList = new ArrayList<Present>();
			try {
				presentList = presentService.getAllPresentByCondition(" where year(presentDate)='" + y
						+ "' and month(presentDate)='" + m + "' and presentType='" + type + "'");
			} catch (Exception e) {
				log.info("【财务管理/记账本】查询当前年月记账记录异常！" + e.toString());
			}
			Iterator<?> it1 = presentList.iterator();
			while (it1.hasNext()) {
				present = (Present) it1.next();

				String presentExpense = "";
				if (present.getPresentExpenses().equals("") || present.getPresentExpenses().equals(null)) {
					presentExpense = "0.00";
				} else {
					presentExpense = present.getPresentExpenses();
				}
				// 计算当前月支出和
				typeTotal = typeTotal + Double.parseDouble(presentExpense);
			}

			param[i] = if1ZeroTo2Zero(new DecimalFormat("####.##").format(typeTotal));
		}
		pm = getPresentMonthValue(param);
		// 类型
		pm.setSummary(getTypeNameByTypeId(type));
		return pm;
	}

	// 合计
	public PresentMonth getPresentMonthByYearAndMonth() {
		String currentDate = CurrentDate.currentDate();
		String currentMonth = currentDate.substring(5, 7);
		String currentYear = currentDate.substring(0, 4);

		int year = Integer.parseInt(currentYear);
		int month = Integer.parseInt(currentMonth);
		String y, m;

		PresentMonth pm = new PresentMonth();
		String[] param = new String[12];

		for (int i = 0; i < 12; i++) {
			double typeTotal = 0.00;
			Present present = new Present();
			if (i != 0) {
				month = month - 1;
			}
			if (month == 0) {
				month = 12;
				year = year - 1;
			}

			y = String.valueOf(year);
			m = String.valueOf(month);
			if (m.length() == 1) {
				m = 0 + m;
			}
			// 当前月
			List<Present> presentList = new ArrayList<Present>();
			try {
				presentList = presentService.getAllPresentByCondition(" where year(presentDate)='" + y
						+ "' and month(presentDate)='" + m + "'");
			} catch (Exception e) {
				log.info("【财务管理/记账本】查询当前年月记账记录异常！" + e.toString());
			}
			Iterator<?> it1 = presentList.iterator();
			while (it1.hasNext()) {
				present = (Present) it1.next();

				String presentExpense = "";
				if (present.getPresentExpenses().equals("") || present.getPresentExpenses().equals(null)) {
					presentExpense = "0.00";
				} else {
					presentExpense = present.getPresentExpenses();
				}
				// 计算当前月支出和
				typeTotal = typeTotal + Double.parseDouble(presentExpense);
			}

			param[i] = if1ZeroTo2Zero(new DecimalFormat("####.##").format(typeTotal));
		}
		pm = getPresentMonthValue(param);
		// 类型
		pm.setSummary("合计");
		return pm;
	}

	// 最近5年商品/网购支出情况汇总
	public PresentMonth getPresentMonthByYear(int year) {
		PresentMonth pm = new PresentMonth();
		String[] param = new String[12];

		for (int i = 1; i <= 12; i++) {
			double typeTotal = 0.00;
			Present present = new Present();

			// 当前月
			List<Present> presentList = new ArrayList<Present>();
			try {
				presentList = presentService.getAllPresentByCondition(" where year(presentDate)='" + String.valueOf(year)
								+ "' and month(presentDate)='" + String.valueOf(i) + "'");
			} catch (Exception e) {
				log.info("【财务管理/记账本】查询当前年月记账记录异常！" + e.toString());
			}
			Iterator<?> it1 = presentList.iterator();
			while (it1.hasNext()) {
				present = (Present) it1.next();

				String presentExpense = "";
				if (present.getPresentExpenses().equals("") || present.getPresentExpenses().equals(null)) {
					presentExpense = "0.00";
				} else {
					presentExpense = present.getPresentExpenses();
				}
				// 计算当前月支出和
				typeTotal = typeTotal + Double.parseDouble(presentExpense);
			}

			param[i - 1] = if1ZeroTo2Zero(new DecimalFormat("####.##").format(typeTotal));
		}
		pm = getPresentMonthValue(param);
		// 类型
		pm.setSummary(String.valueOf(year));
		return pm;
	}

	// 赋值
	public PresentMonth getPresentMonthValue(String[] param) {
		PresentMonth pm = new PresentMonth();
		pm.setParam1(param[0]);
		pm.setParam2(param[1]);
		pm.setParam3(param[2]);
		pm.setParam4(param[3]);
		pm.setParam5(param[4]);
		pm.setParam6(param[5]);
		pm.setParam7(param[6]);
		pm.setParam8(param[7]);
		pm.setParam9(param[8]);
		pm.setParam10(param[9]);
		pm.setParam11(param[10]);
		pm.setParam12(param[11]);
		return pm;
	}

	public String getTypeNameByTypeId(String type) {
		String typeName = "";
		if (type.equals("0")) {
			typeName = "服饰鞋帽";
		} else if (type.equals("1")) {
			typeName = "食品茶饮";
		} else if (type.equals("2")) {
			typeName = "日常用品";
		} else if (type.equals("3")) {
			typeName = "数码家电";
		} else {
			typeName = "其他";
		}
		return typeName;
	}

	public PresentMonth getPresentMonth() {
		String currentDate = CurrentDate.currentDate();
		String currentMonth = currentDate.substring(5, 7);
		String currentYear = currentDate.substring(0, 4);
		int month = Integer.parseInt(currentMonth);
		int year = Integer.parseInt(currentYear);
		String y = "2012", m = "01";

		PresentMonth pm = new PresentMonth();
		String[] param = new String[12];
		for (int i = 0; i < 12; i++) {
			if (i != 0) {
				month = month - 1;
			}
			if (month == 0) {
				month = 12;
				year = year - 1;
			}

			m = String.valueOf(month);
			y = String.valueOf(year);
			if (m.length() == 1) {
				m = 0 + m;
			}
			param[i] = y + m;
		}
		pm = getPresentMonthValue(param);
		pm.setSummary("0");

		return pm;
	}

	public PresentMonth getMonth() {
		String m = "";

		PresentMonth pm = new PresentMonth();
		String[] param = new String[12];
		for (int i = 1; i <= 12; i++) {
			m = String.valueOf(i);
			if (m.length() == 1) {
				m = 0 + m;
			}
			param[i - 1] = m;
		}
		pm = getPresentMonthValue(param);
		pm.setSummary("0");

		return pm;
	}

	public String transformMonth(int mon) {
		String str = "";
		switch (mon) {
		case 1:
			str = "一月";
			break;
		case 2:
			str = "二月";
			break;
		case 3:
			str = "三月";
			break;
		case 4:
			str = "四月";
			break;
		case 5:
			str = "五月";
			break;
		case 6:
			str = "六月";
			break;
		case 7:
			str = "七月";
			break;
		case 8:
			str = "八月";
			break;
		case 9:
			str = "九月";
			break;
		case 10:
			str = "十月";
			break;
		case 11:
			str = "十一月";
			break;
		case 12:
			str = "十二月";
			break;
		default:
			str = "default";
		}
		return str;
	}

	/**
	 * 判断金额后面1个0还是2个0，如果1个0或者没有0，则补全到2个0，例如100.1，变成100.10
	 * 
	 * @param str
	 */
	public String if1ZeroTo2Zero(String str) {
		if (str.indexOf(".") != -1) {
			if (str.substring(str.lastIndexOf(".")).length() == 2) {// 1.0
				str = str + "0";
			} else if (str.substring(str.lastIndexOf(".")).length() > 3) {// 1.000,截取小数点后两位
				str = str.substring(0, str.lastIndexOf(".") + 3);
			} else if (str.substring(str.lastIndexOf(".")).length() < 2) {// 1.
				str = str + "00";
			} else {// ==3,1.00
			}
		} else {
			str = str + ".00";
		}
		if (str.equals("0.00")) {
			str = "";
		}
		return str;
	}

	@Test
	public void testAA() {
		/*
		 * List<PresentMonth> presentMonthList = getPresentOfLastFiveYear();
		 * for(int i=0;i<presentMonthList.size();i++) { PresentMonth pm =
		 * presentMonthList.get(i); System.out.println(pm.getSummary());
		 * 
		 * }
		 */

		PresentMonth presentMonth = getPresentMonthByYear(2013);
		System.out.println(presentMonth.getSummary());

	}
}
